package org.oneblog.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.oneblog.bean.ImageConfiger;
import org.oneblog.bean.ImagePath;
import org.oneblog.data.util.StringTools;
import org.oneblog.service.AbstractImageService;
import org.oneblog.service.IImageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

//图片处理类
@Service("imageService")
public class ImageServiceImpl extends AbstractImageService implements IImageService {

    //静态变量无法注入
    @Value("${system.img.basePath}")
    private String BASE_PATH = "";

    @Override
    public String uploadImageWithFixedScale(File srcFile, ImageConfiger imageConfiger) {
        
        try {
            //上传小图，格式化大小的；最大宽度 840
            return uploadImageWithFixedScale(new FileInputStream(srcFile), imageConfiger);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String uploadImageWithThumbnail(File srcFile, ImageConfiger imageConfiger) {
        
        try {
            //上传小图，格式化大小的；最大宽度 260
            return uploadImageWithThumbnail(new FileInputStream(srcFile), imageConfiger);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public String uploadImageWithFixedScale(InputStream inputStream, ImageConfiger imageConfiger) {
        /*
         * 1、确认当前时间对应目录是否生成
         * 
         * 2、生成文件上传之后的文件名
         * 
         * 3、执行相关操作
         */
        String fileName = System.currentTimeMillis() + "_" + StringTools.randomUUID() + ".jpg";
        
        ImagePath imagePath = ensureFolderPath(BASE_PATH);
        
        System.out.println("imagePath : " + JSON.toJSONString(imagePath));
        
        String relativePath = imagePath.getRelativePath() + fileName;
        
        String fileFullPath = BASE_PATH + relativePath;
        
        //上传小图，格式化大小的；最大宽度 840
        imageConfiger.setTargetWidth(1240);
        
        scaleWithWidth(inputStream, fileFullPath, imageConfiger, null);
        
        return relativePath;
    }
    
    @Override
    public String uploadImageWithThumbnail(InputStream inputStream, ImageConfiger imageConfiger) {
        
        String fileName = System.currentTimeMillis() + "_" + StringTools.randomUUID() + ".jpg";

        ImagePath imagePath = ensureFolderPath(BASE_PATH);
        
        String relativePath = imagePath.getRelativePath() + fileName;
        
        String fileFullPath = BASE_PATH + relativePath;
        
        //上传小图，格式化大小的；最大宽度 260
        imageConfiger.setTargetWidth(260);
        scaleWithWidth(inputStream, fileFullPath, imageConfiger, null);
        
        return relativePath;
    }
    
    @Override
    public FileInputStream readImageByRelativePath(String path) {
        try {
            File file = new File(BASE_PATH + path);
            
            if(!file.exists()) {
                return null;
            }
            
            return new FileInputStream(file);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
